<?php
/**
 * CreateTime: 2023/2/27 10:24
 */

namespace App\Http\Controllers\MeMediator;

use App\Consts\BaseConsts\BaseConsts;
use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\Func\TokenFuc;
use App\Http\Controllers\Controller;
use App\Http\Requests\ListBasicRequest;
use App\Http\Requests\MeMediator\AddMeMediatorVerbalTrickRequest;
use App\Models\MeMediator\MeMediatorVerbalTrickModel;

class MeMediatorVerbalTrickController extends Controller
{
    public function addVerbalTrick(AddMeMediatorVerbalTrickRequest $request){
        $data = $request->all();
        $staffInfo = TokenFuc::getCurrentStaffInfo();
        $data['owner_id'] = $staffInfo['staff_id'];
        $res = (new MeMediatorVerbalTrickModel())->add($data);
        if($res){
            return ResponseFunc::returnJson(
                'ok',
                null,
                '添加话术成功',
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
            );
        }
        return ResponseFunc::returnJson(
            'error',
            null,
            '添加话术失败',
            ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
        );
    }

    public function getVerbalTrick(ListBasicRequest $request){
        $pFilters = $request->get('filters');
        $pageSize = $request->get('page_size');
        $filters = $pFilters ? json_decode($pFilters,true) : [];
        $model = new MeMediatorVerbalTrickModel();
        $query = $model
            ->select([
                'verbal_trick_id',
                'vt_content',
                'vt_type',
                'owner_id',
                'owner_id as owner_str',
            ])->where('status',BaseConsts::STATUS_NORMAL);
        if($filters) {
            if (isset($filters['vt_type']) && $filters['vt_type']) {
                $query = $query->where('vt_type', $filters['vt_type']);
            }
            if(isset($filters['vt_content']) && $filters['vt_content']){
                $query = $query->where('vt_content','LIKE',"%".$filters['vt_content']."%");
            }
        }
        $data = $model->getListNoTime($query,$pageSize) ?? [];
        return ResponseFunc::returnJson(
            'ok',
            $data,
            '获取话术列表成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );

    }

    public function getVerbalTrickInfo($vtId){
        $model = new MeMediatorVerbalTrickModel();
        $data = $model->getById($vtId,[
            'verbal_trick_id',
            'vt_content',
            'vt_type',
            'owner_id',
            'owner_id as owner_str',
        ]);
        return ResponseFunc::returnJson(
            'ok',
            $data,
            '获取话术详情成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }


    public function editVerbalTrick($vtId){
        $data = request()->all();
        unset($data['status']);
        unset($data['created_time']);
        unset($data['updated_time']);
        $data['verbal_trick_id'] = $vtId;
        try {
            $res = (new MeMediatorVerbalTrickModel())->updateData($data);
            return ResponseFunc::returnJson(
                'ok',
                $data,
                '编辑话术成功',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS
            );
        }catch (\Exception $exception){
            return ResponseFunc::returnJson(
                'error',
                $data,
                '编辑话术失败',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS
            );
        }

    }

    public function deleteVerbalTrick($vtId){
        $data = [
           'verbal_trick_id'=> $vtId,
           'status' => BaseConsts::STATUS_DELETE
        ];
        try {
            $res = (new MeMediatorVerbalTrickModel())->updateData($data);
            return ResponseFunc::returnJson(
                'ok',
                $data,
                '删除话术成功',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS
            );
        }catch (\Exception $exception){
            return ResponseFunc::returnJson(
                'error',
                $data,
                '删除话术失败',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS
            );
        }
    }

}